local skill = fk.CreateSkill {
  name = "#integrity__precept_skill",
  attached_equip = "integrity__precept",
}

Fk:loadTranslationTable{
  ["#integrity__precept_skill"] = "信戒·辱律",
  ["@@integrity__precept-turn"] = "信戒·辱律",
  ["#integrity__precept-duel"] = "信戒·辱律：请视为使用【决斗】",
  ["#integrity__precept-invoke"] = "是否废除防具栏？",
}

skill:addEffect(fk.Damaged, {
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skill.name)
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = table.filter(player:getEquipments(Card.SubtypeArmor), function (id)
      return Fk:getCardById(id).name == skill.attached_equip
    end)
    if #cards > 0 then
      room:throwCard(cards, skill.name, player, player)
    end
  end,
})

local spec = {
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = table.filter(player:getCardIds("h"), function (id)
      return Fk:getCardById(id).trueName == "slash" and not player:prohibitDiscard(id)
    end)
    if #cards > 0 then
      room:throwCard(cards, skill.name, player, player)
      if player.dead then return end
    end
    room:askToUseVirtualCard(player, {
      name = "duel",
      skill_name = skill.name,
      prompt = "#integrity__precept-duel",
      cancelable = false,
    })
    if player.dead then return end
    if #player:getAvailableEquipSlots(Card.SubtypeArmor) > 0 and
      room:askToSkillInvoke(player, {
        skill_name = skill.name,
        prompt = "#integrity__precept-invoke",
      }) then
      room:abortPlayerArea(player, Player.ArmorSlot)
    end
  end,
}

skill:addEffect(fk.AfterCardsMove, {
  priority = 0,
  can_trigger = function(self, event, target, player, data)
    if player.room:getBanner("hanqing__gujie") and
      table.find(player.room.void, function (id)
        return Fk:getCardById(id).name == skill.attached_equip
      end) then
      for _, move in ipairs(data) do
        if move.from and move.toArea == Card.Void then
          if move.from == player and not player.dead then
            for _, info in ipairs(move.moveInfo) do
              if info.fromArea == Card.PlayerEquip and
                Fk:getCardById(info.cardId).name == skill.attached_equip then
                return true
              end
            end
          end
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = spec.on_use,
})

skill:addEffect(fk.Damaged, {
  can_trigger = function(self, event, target, player, data)
    return target == player and not player.dead and
      table.contains(player.sealedSlots, Player.ArmorSlot) and player.room:getBanner("hanqing__duoye")
  end,
  on_cost = Util.TrueFunc,
  on_use = spec.on_use,
})

skill:addEffect(fk.BeforeCardsMove, {
  can_refresh = function (self, event, target, player, data)
    for _, move in ipairs(data) do
      if move.from == player then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerEquip and
            Fk:getCardById(info.cardId).name == skill.attached_equip then
            return true
          end
        end
      end
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local new_data = {}
    for _, move in ipairs(data) do
      local new_move = {}
      if #move.moveInfo > 0 and move.toArea ~= Card.Void then
        local orig_info, new_info = {}, {}
        for i = 1, #move.moveInfo do
          local info = move.moveInfo[i]
          local will_destruct = false
          if info.fromArea == Card.PlayerEquip and Fk:getCardById(info.cardId).name == skill.attached_equip then
            will_destruct = true
          end
          if will_destruct then
            table.insert(new_info, info)
          else
            table.insert(orig_info, info)
          end
        end
        move.moveInfo = orig_info
        if #new_info > 0 then
          new_move = {
            moveInfo = new_info,
            from = player,
            to = nil,
            toArea = Card.Void,
            moveReason = fk.ReasonJustMove,
            proposer = nil,
            skillName = skill.name,
            moveVisible = true,
          }
        end
      end
      if next(new_move) then
        table.insert(new_data, new_move)
      end
    end
    if next(new_data) then
      table.insertTable(data, new_data)
    end
  end,
})

return skill
